package top.kikt.book.core.handlers

import org.jsoup.nodes.Document
import top.kikt.book.core.entity.BookMetaEntity
import top.kikt.book.core.entity.ChapterEntity
import top.kikt.book.core.entity.ChapterMetaEntity
import top.kikt.book.core.handlers.common.BaseBookHandler
import top.kikt.book.util.bookText
import top.kikt.book.util.getUrl
import top.kikt.book.util.metaContentWithProperty
import java.nio.charset.Charset

@Suppress("ClassName")
class `93mc` : BaseBookHandler() {
    override val host: String
        get() = "www.93mc.com"

    override fun forceCharset(): Charset {
        return Charset.forName("GBK")
    }

    override fun parseBookMeta(document: Document, url: String): BookMetaEntity {
        val title = document.metaContentWithProperty("og:novel:book_name")
        val author = document.metaContentWithProperty("og:novel:author")
        val cover = document.metaContentWithProperty("og:image")
        return BookMetaEntity(title, url, url, author, cover)
    }

    override fun parseChapterMeta(document: Document, menuUrl: String): List<ChapterMetaEntity> {
        val list = document.select("#list-chapterAll dl dd")

        list.forEach(::println)

        return list.mapIndexed { index, it ->
            val a = it.selectFirst("a")
            val url = a!!.getUrl(menuUrl)
            val name = a.text()
            ChapterMetaEntity(name, index, url, it)
        }
    }

    override fun parseChapter(chapterMetaEntity: ChapterMetaEntity): ChapterEntity? {
        val doc = chapterMetaEntity.toDocument(forceCharset()) ?: return null
        val text = doc.select("#htmlContent").bookText()
        return chapterMetaEntity.toChapterMetaEntity(text)
    }

}